package com.loveprogrammer.handler.support;

import com.alibaba.fastjson2.JSON;
import com.loveprogrammer.command.anotation.TagListener;
import com.loveprogrammer.command.anotation.TopicListener;
import com.loveprogrammer.command.client.ClientSceneTag;
import com.loveprogrammer.command.client.ClientTopic;
import com.loveprogrammer.command.client.ClientMenuTag;
import com.loveprogrammer.command.server.ServerBattleTag;
import com.loveprogrammer.command.server.ServerHeroTag;
import com.loveprogrammer.command.server.ServerTopic;
import com.loveprogrammer.console.ConsolePrint;
import com.loveprogrammer.constants.CommonValue;
import com.loveprogrammer.dto.base.StringRequest;
import com.loveprogrammer.dto.battle.FightChoose;
import com.loveprogrammer.dto.battle.MapChoose;
import com.loveprogrammer.handler.BaseHandler;
import com.loveprogrammer.network.NetworkClientListener;
import com.loveprogrammer.pojo.StringMessage;
import com.loveprogrammer.utils.ChannelUtils;
import com.loveprogrammer.utils.ScannerInput;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Scanner;

/**
 * @ClassName MenuHandler
 * @Description TODO
 * @Author admin
 * @Date 2024/2/18 17:37
 * @Version 1.0
 */
@TopicListener(topic = ClientTopic.TOPIC_MENU)
public class MenuHandler extends BaseHandler {

    public static final Logger log = LoggerFactory.getLogger(MenuHandler.class);

    @TagListener(tag = ClientMenuTag.TAG_MENU_PORTAL,messageClass = StringRequest.class)
    public void portalMenu(ChannelHandlerContext ctx, StringRequest msg){
        ConsolePrint.publishMessage(msg.getContent());
        // 展示首页数据
        ConsolePrint.publishMessage("请选择您要进行的操作");
        ConsolePrint.publishMessage("【1.打怪】  【2.属性及装备】  【3.战兽】");
        ConsolePrint.publishMessage("【4.冒险者工会】   【5.副本】  【6.工会】 ");
        ConsolePrint.publishMessage("【8.配置】  【9.退出】");
        ConsolePrint.publishMessage("请选择：");

        int choose = ScannerInput.inputInt(1, 9, 9);

        if (choose != 9) {
            switch (choose) {
                case 1:
                    // 请求地图列表，同时跳转到地图菜单
                    StringMessage message = StringMessage.create(ServerTopic.TOPIC_BATTLE, ServerBattleTag.TAG_BATTLE_MAPS);
                    message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
                    StringRequest request = new StringRequest(ClientTopic.TOPIC_MENU,ClientMenuTag.TAG_MENU_MAP_CHOOSE, ClientTopic.TOPIC_MENU,ClientMenuTag.TAG_MENU_PORTAL);
                    message.setBody(JSON.toJSONString(request));
                    ChannelUtils.pushToServer(ctx.channel(), message);
                    break;
                case 2:
                    // 请求英雄数据
                    message = StringMessage.create(ServerTopic.TOPIC_HERO, ServerHeroTag.TAG_HERO_INFO);
                    message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
                    request = new StringRequest(ClientTopic.TOPIC_SCENE,ClientSceneTag.TAG_SCENE_HERO, ClientTopic.TOPIC_MENU,ClientMenuTag.TAG_MENU_PORTAL);
                    message.setBody(JSON.toJSONString(request));
                    ChannelUtils.pushToServer(ctx.channel(), message);

                case 3:

                case 4:

                case 5:

                case 6:

                case 7:

                case 8:

                default:
                    ConsolePrint.publishMessage("暂未开放，敬请期待", 1);
                    break;
            }
        }
        else {
            // 这里不退出，而是返回首页，做一个重定向
            NetworkClientListener.getInstance().forward(ctx,ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_PORTAL, JSON.toJSONString(msg));
        }
    }

    /***
     * 选择地图
     * @param ctx
     * @param data
     */
    @TagListener(tag = ClientMenuTag.TAG_MENU_MAP_CHOOSE, messageClass = MapChoose.class)
    public void chooseMaps(ChannelHandlerContext ctx, MapChoose data) {
        //
        ConsolePrint.publishMessage("请选择要进入的地图, 0 退出");
        List<MapChoose.MapItem> mapList = data.getMapList();
        for (int i = 0; i < mapList.size(); i++) {
            ConsolePrint.publishMessage((i+1) + ":" + mapList.get(i).toString(), 1);
        }
        int choose = ScannerInput.inputInt(0, mapList.size(), 0);
        if(choose != 0) {
            MapChoose.MapItem item = mapList.get(choose - 1);
            ConsolePrint.publishMessage("选择的地图为" + item.getName());

            // 继续选择是打怪还是返回
            ConsolePrint.publishMessage("请选择您要进行的操作");
            ConsolePrint.publishMessage("【1.打怪寻宝】  【2.挑战BOSS】  【3.返回】");
            choose = ScannerInput.inputInt(1, 3, 3);
            if (choose != 3) {
                switch (choose) {
                    case 1:
                        // 战斗
                        StringMessage message = StringMessage.create(ServerTopic.TOPIC_BATTLE, ServerBattleTag.TAG_BATTLE_COMMON_FIGHT);
                        message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
                        FightChoose request = new FightChoose(ClientTopic.TOPIC_SCENE, ClientSceneTag.TAG_SCENE_BATTLE, ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_MAP_CHOOSE);
                        request.setMapId(item.getId());
                        request.setType(1);
                        message.setBody(JSON.toJSONString(request));
                        ChannelUtils.pushToServer(ctx.channel(), message);
                        break;
                    case 2:
                        // 战斗
                        ConsolePrint.publishMessage("暂未开放，敬请期待", 1);
                        // break;
                    default:
                        // 返回上一页
                        NetworkClientListener.getInstance().forward(ctx, ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_MAP_CHOOSE, JSON.toJSONString(data));
                }
            } else {
                // 返回上一页
                NetworkClientListener.getInstance().forward(ctx, ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_MAP_CHOOSE, JSON.toJSONString(data));
            }
        } else {
            // 返回上一页
            NetworkClientListener.getInstance().forward(ctx, ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_MAP_CHOOSE, JSON.toJSONString(data));
        }
    }
}
